package cc.aiee.core.kit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharsetUtil;

public class KitSignature {

	public static String mapToLinkString(Map<String, Object> params, String connStr, String encode,
			List<String> ignoreKeys) {
		List<String> keys = new ArrayList<>(params.keySet());
		Collections.sort(keys);
		StringBuilder linkStr = new StringBuilder();

		boolean isIgnore = ignoreKeys == null || ignoreKeys.isEmpty() ? false : true;

		String key, value;
		for (int i = 0; i < keys.size(); i++) {
			key = keys.get(i);
			value = MapUtil.getStr(params, key);

			if (isIgnore && ignoreKeys.indexOf(key) > -1) {
				continue;
			}

			linkStr.append(key).append("=").append(encode == null ? value : KitURL.urlEncode(value, encode));
			// 拼接时，不包括最后一个&字符
			if (i != keys.size() - 1) {
				linkStr.append(connStr);
			}
		}
		return linkStr.toString();
	}

	public static String mapToLinkString(Map<String, Object> params, List<String> ignoreKeys) {
		return mapToLinkString(params, "&", CharsetUtil.UTF_8, ignoreKeys);
	}

	public static String mapToLinkString(Map<String, Object> params) {
		return mapToLinkString(params, "&", CharsetUtil.UTF_8, null);
	}
}
